home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / procvardemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-09  |  3.7 KB  |  115 lines

  1. {
  2. GPC demo program about BP compatible procedural variables (including
  3. parameters), Standard Pascal procedural parameters, and procedure
  4. pointers (variables and parameters), a GPC extension.
  5.  
  6. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  7.  
  8. Author: Frank Heckenbach <frank@pascal.gnu.de>
  9.  
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, version 2.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; see the file COPYING. If not, write to
  21. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. Boston, MA 02111-1307, USA.
  23.  
  24. As a special exception, if you incorporate even large parts of the
  25. code of this demo program into another program with substantially
  26. different functionality, this does not cause the other program to
  27. be covered by the GNU General Public License. This exception does
  28. not however invalidate any other reasons why it might be covered
  29. by the GNU General Public License.
  30. }
  31.  
  32. program ProcVarDemo;
  33.  
  34. { A demo procedure }
  35. procedure Foo (a : Integer);
  36. begin
  37.   Writeln ('Foo ', a)
  38. end;
  39.  
  40. type
  41.   BPProcType = procedure (a : Integer);  { BP compatible }
  42.   GPCProcPtr = ^procedure (a : Integer);  { GPC extension }
  43.  
  44. var
  45.   BPProcVar : BPProcType;
  46.   BPProcVarAsPointer : Pointer absolute BPProcVar;
  47.   GPCProcPtrVar : GPCProcPtr;
  48.   GPCProcPtrVarAsPointer : Pointer absolute GPCProcPtrVar;
  49.  
  50. procedure SPProcPar (procedure Proc (a : Integer));
  51. begin
  52.   Proc (3)
  53. end;
  54.  
  55. procedure BPProcPar (Proc : BPProcType);
  56. begin
  57.   Proc (4)
  58. end;
  59.  
  60. procedure GPCProcPar (Proc : GPCProcPtr);
  61. begin
  62.   Proc^ (5)
  63. end;
  64.  
  65. begin
  66.   Writeln ('The output of this demo is not very meaningful.');
  67.   Writeln ('Please read the comments in the source code.');
  68.  
  69.   { Procedural variables. GPC supports BP compatible procedural
  70.     variables as well as procedure pointers as an extension. All
  71.     that's said here applies to procedures and functions with any
  72.     number and types of arguments, of course. }
  73.  
  74.   { BP compatible }
  75.   { Assigning them and calling them works without any `@' or `^' and
  76.     may therefore seem easier... }
  77.   BPProcVar := Foo;
  78.   BPProcVar (1);
  79.   { ...however, when you have to compare them to pointers, or need
  80.     the address of the variable itself, you need an extra `@' which
  81.     makes for some rather strange syntax... }
  82.   Writeln (@BPProcVar <> nil);
  83.   Writeln (@BPProcVar = BPProcVarAsPointer);
  84.   Writeln (Pointer (@@BPProcVar) = Pointer (@BPProcVarAsPointer));
  85.  
  86.   { GPC extension }
  87.   { Procedure pointers. They can be assigned with `@' and used with
  88.     `^' like regular pointers. }
  89.   GPCProcPtrVar := @Foo;
  90.   GPCProcPtrVar^ (2);
  91.   { Therefore, comparing them to other pointers and taking the
  92.     address of them is straightforward. }
  93.   Writeln (GPCProcPtrVar <> nil);
  94.   Writeln (GPCProcPtrVar = GPCProcPtrVarAsPointer);
  95.   Writeln (Pointer (@GPCProcPtrVar) = Pointer (@GPCProcPtrVarAsPointer));
  96.  
  97.   { Procedural parameters. Standard Pascal defines a way to declare
  98.     them. BP does *not* support this, but instead allows its
  99.     procedural variables to be used as parameters. GPC supports both
  100.     as well as procedure pointers used as parameters. }
  101.  
  102.   { Standard Pascal }
  103.   { The type of the procedural parameters is declared within the
  104.     called procedure. }
  105.   SPProcPar (Foo);
  106.  
  107.   { Borland Pascal }
  108.   { Using the declared procedural type as a parameter. }
  109.   BPProcPar (Foo);
  110.  
  111.   { GPC extension }
  112.   { Using the declared procedure pointer type as a parameter. }
  113.   GPCProcPar (@Foo)
  114. end.
  115.